package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CarGeneration {
	
	 Connect con = new Connect(); 
	 Connection conn = con.Connect();
	 ResultSet rs = null;
	 
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 private static final String TABLE_CarGenerations = "_CarGenerations";
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	public ResultSet selectCarGenerations() throws Exception {
		String StrSql   = "SELECT	 [OID]," +
									"[IsActive]," +
									"[IsDeleted]," +
									
									"[CarGenerationCode],"+
									"[CarGenerationName],"+
									"[CarBandOID],"+
									"[CarSize],"+
									"[CarGroupOID]" +		
                			 
                		  "FROM		 [dbo].[CarGenerations] " +
                		  
                		  "WHERE	 [IsDeleted]='0' "; 
		
		StrSql += "ORDER BY [CarGenerationName] ASC ";
		
		return con.selectData(StrSql);
	 }
	
	 public void closeConnecttion() throws Exception {
    	 if (conn != null) {
             conn.close();
         }
    }
	 
	 public void DEFINE_CarGenerations() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			try {
				rs = selectCarGenerations();
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("CarGenerationsOID", rs.getString("OID"));
					Val.put("CarGenerationCode", rs.getString("CarGenerationCode"));
					Val.put("CarGenerationName", rs.getString("CarGenerationName"));
					Val.put("CarBandOID", rs.getString("CarBandOID"));
					Val.put("CarSize", rs.getString("CarSize"));
					Val.put("CarGroupOID", rs.getString("CarGroupOID"));

					db.insert(TABLE_CarGenerations, null, Val);
				}
			} catch (Exception e) {
				Log.d("TABLE_CarGenerations", "Create Table not Successfully.");
			}finally{
				try {
					closeConnecttion();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Log.d("TABLE_CarGenerations", "Create Table Successfully.");
			db.close();
		}
	 
	 public String[][] SelectAll_CarGenerations(String StrCarGroupOID,
				String StrCarBandsOID) throws Exception{
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				String strSQL = "SELECT  CarGenerationsOID As _id , * FROM "
						+ TABLE_CarGenerations;
				// +" WHERE [CarGroupOID] LIKE '" + "130201302010022" + "' ";
				// +" AND [CarBandOID] LIKE '" + "139201310070002" + "' ";
				strSQL += " WHERE [CarBandOID] LIKE '" + StrCarBandsOID + "' ";
				Cursor cursor = db.rawQuery(strSQL, null);

				//
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);
							arrData[i][5] = cursor.getString(6);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}
}
